<template>
  <div class="goods">
    <base-template :breadList="['商品管理', '商品列表']">
      <el-row class="nav_goods" type="flex" justify="space-between">
        <div class="nav_left">
          <el-input class="search" v-model="paginationInfo.query" @change="searchInputValue" clearable></el-input>
          <el-button class="search el-icon-search" @click="searchInputValue"></el-button>
        </div>
        <el-button type="primary" class="el-icon-plus" @click="addGoods">添加商品</el-button>
      </el-row>

      <el-table :data="goodsData" border stripe>
        <el-table-column type="index" label="#" width="60px"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="170px">
          <template v-slot="scope">
            {{ new Date(scope.row.add_time).toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template v-slot="scope">
            <el-button size="mini" type="primary" class="el-icon-edit marginRight">编辑</el-button>

            <el-popconfirm title="确定删除吗？" @confirm='removeGoods(scope.row)'>
              <el-button  size="mini" type="danger" class="el-icon-delete"  slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paginationInfo.pagenum"
        :page-sizes="[2, 5, 10, 50]"
        :page-size="paginationInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :pager-count="5"
      >
      </el-pagination>
    </base-template>
  </div>
</template>

<script>
import BaseTemplate from '../../component/base-template.vue'
export default {
  components: {
    BaseTemplate,
  },
  data() {
    return {
      goodsData: [],
      paginationInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
    }
  },
  created() {
    this.getGoodsData()
  },
  methods: {
    async getGoodsData() {
      const { total, goods } = await this.$store.dispatch('getRequest', { path: '/goods', params: this.paginationInfo })

      this.total = total
      this.goodsData = goods
    },
    handleSizeChange(val) {
      //每页
      this.paginationInfo.pagesize = val
      this.getGoodsData()
    },
    handleCurrentChange(val) {
      //当前页
      this.paginationInfo.pagenum = val
      this.getGoodsData()
    },
    async searchInputValue() {
      this.paginationInfo.pagenum = 1
      const { total, goods } = await this.$store.dispatch('getRequest', { path: '/goods', params: this.paginationInfo })

      this.goodsData = goods
      this.total = total
    },
    async removeGoods(row) {
      const res = await this.$store.dispatch('deleteRequest', { path: `/goods/${row.goods_id}` })
      if (res.status == 200) {
        this.getGoodsData()
        this.$message.success('删除成功')
      } else {
        this.$message.error('删除失败')
      }
    },
    addGoods() {
      this.$router.push('/home/addGoods')
    },
  },
}
</script>

<style lang="less" scoped></style>
